

import axios from 'axios'
import {useMemberStore} from "@/store/modules/member";
import {useUserStore} from "@/store/modules/user";
// 获取会员在pinia对象
const { memberToken } = useMemberStore()
// 获取系统用户在pinia对象
const { token } = useUserStore()


async function  apiHttp({url, method, param}: { url: string, method: string, param: any }){
    let paramDate=param;
    let requestUrl=import.meta.env.VITE_APP_BASE_API+url;
    if (method!="POST"){
        requestUrl=requestUrl+jsonToGetParams(param);
        paramDate={};
    }
    const {data} = await axios({
        url: requestUrl,
        method: method || 'GET',
        data: paramDate,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
            'memberToken': memberToken,
            'userToken': token,
            'token': memberToken,
        }
    })
    return data
}
export {apiHttp}




/**
 * GET参数转字符串
 * @param jsonObj
 * @returns {string}
 */
export function jsonToGetParams(jsonObj:Object) {
    const params = Object.entries(jsonObj)
        .map(([key, value]) => `${key}=${value}`)
        .join('&');
    if (params.length >= 1) {
        return "?" + params
    } else {
        return params;
    }
}
